package com.sgl.gk.controller;

import com.sgl.common.annotation.Log;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.enums.BusinessType;
import com.sgl.common.utils.ServletUtils;
import com.sgl.gk.controller.vo.GkStationVo;
import com.sgl.gk.domain.GkStation;
import com.sgl.gk.service.GkStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 *
 * 工位管理的controller
 **/
@RestController
@RequestMapping("/gk/station")
public class GkStationController extends BaseController {

    @Autowired
    GkStationService gkStationService;

    /**
     * 查询工位信息
     * @param gkStation 查询条件
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(GkStation gkStation){
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        Map<String,Object> map = new HashMap<>();
        List<GkStationVo> list = gkStationService.list(gkStation);
        map.put("data",getDataTable(list));
        int noConfigure = 0;
        for (GkStationVo gkStationVo : list) {
            if (gkStationVo.getConfigureType()==2) {
                noConfigure+=1;
            }
        }
        if (gkStation.getConfigureType()!=null&&gkStation.getConfigureType()==2){
            map.put("allCount",gkStationService.countAll());
        }else {
            map.put("allCount",getDataTable(list).getTotal());
        }
        map.put("noConfigure",noConfigure);
        return AjaxResult.success(map);
    }

    /**
     * 添加工位信息
     * @param gkStation
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @Log(title = "工位管理",businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody GkStation gkStation){
        return gkStationService.add(gkStation);
    }

    /**
     * 修改工位信息
     * @param gkStation
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    @Log(title = "工位管理",businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody GkStation gkStation){
        return gkStationService.edit(gkStation);
    }

    /**
     * 删除工位信息
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @ResponseBody
    @Log(title = "工位管理",businessType = BusinessType.DELETE)
    public AjaxResult removeByIds(@PathVariable("ids") Long[] ids){
        return gkStationService.removeByIds(ids);
    }


    /**
     * 工位管理的一键配置接口
     * @param ids
     * @return
     */
    @RequestMapping(value = {"/oneClickConfiguration/{ids}"})
    @Log(title = "工位配置")
    public AjaxResult oneClickConfiguration(@PathVariable(value = "ids") Long[] ids){
        return gkStationService.oneClickConfiguration(ids);
    }
}
